﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DomainModel.Abstract;
using DomainModel.Entities;
using System.Data.Linq;

namespace DomainModel.Concrete
{
    // Implementation of the Answers section of the database.
    // The middle layer of the DAL.
    class SqlAnswersRepository : IAnswersRepository
    {
        private Table<Answer> answersTable;

        public SqlAnswersRepository(string connectionString)
        {
            answersTable = (new DataContext(connectionString)).GetTable<Answer>();
        }

        public IQueryable<Answer> Answers
        {
            get { return answersTable; }
        }

        public void SaveAnswer(Answer answer)
        {
            if (answer.AnswerID == 0)
            {
                answersTable.InsertOnSubmit(answer);
            }
            else
            {
                answersTable.Attach(answer);
                answersTable.Context.Refresh(RefreshMode.KeepCurrentValues, answer);
            }

            answersTable.Context.SubmitChanges();
        }

        public void DeleteAnswer(Answer answer)
        {
            answersTable.DeleteOnSubmit(answer);
            answersTable.Context.SubmitChanges();
        }
    }
}